package cn.itzzx.lagou.consumer.filter;

import cn.itzzx.lagou.consumer.cache.MonitorCachce;
import cn.itzzx.lagou.consumer.time.StopTimer;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        StopTimer stopTimer = new StopTimer();
        stopTimer.start();
        //调用的方法名称
        String methodName = invocation.getMethodName();
        try {
            return invoker.invoke(invocation);
        } finally {
            stopTimer.stop();
            MonitorCachce.add(methodName, stopTimer.statisticsTime());
        }
    }
}
